//app.js
App({
  onLaunch: function () {
    wx.cloud.init({
      env: 'database-2p2zg',
      traceUser: true
    })
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    if (wx.getStorageSync('user')==undefined) {wx.setStorageSync('user', {})}
    
    if (!wx.getStorageSync('openID')){
      wx.cloud.callFunction({
        name:'postOpenid',
        success:res=>{
          wx.setStorageSync('openID', res.result.openId)
        }
      })
    }
    
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              wx.setStorageSync('uerInfo', res.userInfo)
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  globalData: {
    userInfo: '',
    openID:''
  }
})

/** 小程序代码分包
 * 
 * "pages": [
    "pages/index/index",
    "pages/news/news",
    "pages/me/me"],
    "independent": true,
    "subpackages":[
      {
        "root":"me",
        "pages":[
          "pages/me/me/history/history",
          "pages/me/me/comment-history/comment-history",
          "pages/me/me/suggestion/suggestion",
          "pages/me/me/about/about",
          "pages/me/me/share/share",
          "pages/editMe/editMe"
        ],
        "independent": true
      },
      {
        "root":"details",
        "pages":[
          "pages/details/details",
          "pages/report/report",
          "pages/personal/personal"
        ],
        "independent": true
      },
      {
        "root":"module",
        "pages":[
          "pages/ordinary/ordinary",
          "pages/knowledge/knowledge",
          "pages/social/social",
          "pages/message/message"
        ],
        "independent": true
      }
  ],
 */